<?php
	class User extends MY_controller{

		function __construct(){
			parent::__construct();
			$this->load->model('users');
		}

		function index($nick = 0, $start_time = 0, $end_time = 0, $default_child = 'all', $user_type = 0, $limit = 0){
			if($_POST){
				$post_data = $this->input->post();
				$nick = !empty($post_data['nick']) ? $post_data['nick'] : 0;
				$start_time = !empty($post_data['start_time']) ? strtotime($post_data['start_time']) : 0;
				$end_time = !empty($post_data['end_time']) ? (strtotime($post_data['end_time'])) : 0;
				$default_child = isset($post_data['default_child']) ? $post_data['default_child'] : 'all';
				$user_type = !empty($post_data['user_type']) ? $post_data['user_type'] : 0;
			}
			$conditions['nick'] 			= $nick;
			$conditions['start_time'] 		= $start_time;
			$conditions['end_time'] 		= $end_time + 3600 * 24 - 1;
			$conditions['default_child'] 	= $default_child;
			$conditions['user_type'] 		= $user_type;
			$this->smarty->assign("action", '用户列表');
			$result = $this->users->get_user_list($limit, $this->offset, $conditions);
			$this->load->library('pagination');
			$page_config['base_url'] = '/user/index/'.$nick.'/'.$start_time.'/'.$end_time.'/'.$default_child.'/'.$user_type;
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $this->offset; 
			$page_config['uri_segment'] = 8;
			$this->pagination->initialize($page_config); 
			$pagination = $this->pagination->create_links();

			$today_time = strtotime(date("Y-m-d", time()));
			$yesterday_time = $today_time - 3600 * 24;
			$yesterday_user = get_field_by_where("users", "count(id) AS count", "add_time >= $yesterday_time AND add_time < $today_time ");
			$today_user = get_field_by_where("users", "count(id) AS count", "add_time >= $today_time ");
			$this->smarty->assign("today_user", $today_user['count']);
			$this->smarty->assign("yesterday_user", $yesterday_user['count']);
			$this->smarty->assign("conditions", $conditions);
			$this->smarty->assign("user_list", $result['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->assign("total_count", $result['count']);
			$this->smarty->display("user/user_list.html");
		}

		function add(){
			$this->smarty->assign("action", '添加会员');
			$this->smarty->display("user/user_save.html");
		}

		function visit_statistics($user_id, $start_time = 0, $end_time = 0, $statistics_type = 0){
			if($_POST){
				$post_data = $this->input->post();
				$start_time = !empty($post_data['start_time']) ? $post_data['start_time'] : 0;
				$end_time = !empty($post_data['end_time']) ? $post_data['end_time'] : 0;
				$statistics_type = !empty($post_data['statistics_type']) ? $post_data['statistics_type'] : 1;
			}
			$conditions['start_time'] = $start_time;
			$conditions['end_time'] = $end_time;
			$conditions['statistics_type'] = $statistics_type;
			if(!empty($start_time) || !empty($end_time)){
				$conditions['statistics_type'] = 0;
			}
			
			$this->load->model("User_visit_statistics");
			$result = $this->User_visit_statistics->get_visit_info($user_id, $conditions);
			$visit_statistics_info = $result['result'];
			foreach ($visit_statistics_info as $k => $v) {
				$visit_statistics_info[$k]['visit_url'] = explode("&#&", $v['visit_url']);
				if(count($visit_statistics_info[$k]['visit_url']) > 0){
					foreach ($visit_statistics_info[$k]['visit_url'] as $key => $value) {
						$visit_statistics_info[$k]['visit_url'][$key] = explode("|", $value);
						$sortdistance[$k][$key] = strtotime($visit_statistics_info[$k]['visit_url'][$key][1]);
					}
					array_multisort($sortdistance[$k], SORT_DESC, $visit_statistics_info[$k]['visit_url']);
				}
			}
			$this->smarty->assign("action", '用户轨迹');
			$this->smarty->assign("conditions", $conditions);
			$this->smarty->assign("visit_statistics_info", $visit_statistics_info);
			$this->smarty->display("user/visit_statistics.html");
		}

		function active($type = 1, $limit = 0){
			if($limit < 0) $limit = 0;
			if($type == 1){
				//本周开始时间
				$a_week_time = 3600 * 24 * 7;
				$start_date = $start_time = strtotime(date('Y-m-d', time())) - date('w',strtotime(date('Y-m-d', time())))*86400 + 3600 * 24;
				$end_time = $start_time + 3600 * 24 * 7;
				if($limit > 0){
					$start_date = $start_time = $start_date - $a_week_time * $limit;
					$end_time = $start_time + 3600 * 24 * 7;
				}
			}elseif($type == 2){
				//本月开始时间
				$start_date = $start_time = mktime(0,0,0,date('m'),1,date('Y'));
				//本月结束时间
				$end_time = mktime(0,0,0,date('m'),date('t'),date('Y')) + 3600 * 24;
				if($limit > 0){
					$start_date = $start_time = strtotime(date('Y-m-01 00:00:00',strtotime('-'.$limit.' month')));
					$end_time = strtotime(date("Y-m-d H:i:s",mktime(23,59,59,date("m")-($limit-1) ,0,date("Y"))));
				}
			}
			$today_time = strtotime(date("Y-m-d", time()));
			$active_list = array();
			$weeks = array("周一", "周二", "周三", "周四", "周五", "周六", "周日");
			$i = 0;
			while ($start_time < $end_time) {
				$today_end_time = $start_time + 3600 * 24;
				if($start_time <= $today_time ){
					$reg_user = get_field_by_where("users", "count(id) AS count", "add_time >= $start_time AND add_time < '{$today_end_time}'");
					$active_user = get_field_by_where("user_visit_statistics", "count(id) AS count", "last_time >= $start_time AND last_time < '{$today_end_time}'");
				}else{
					$reg_user['count'] = 0;
					$active_user['count'] = 0;
				}
				$active_list['date'][] = date("Y-m-d", $start_time);
				$active_list['date_time'][] = $start_time;
				$i++;
				$active_list['reg_user'][] = $reg_user['count'];
				$active_list['active_user'][] = $active_user['count'];
				$start_time += 3600 * 24;
			}
			// print_r($active_list);
			if($type == 2 && $limit > 0){
				$this->smarty->assign("echarts_title", date("Y-m-d", $start_date) . '至' . date("Y-m-d", $end_time));
			}else{
				$this->smarty->assign("echarts_title", date("Y-m-d", $start_date) . '至' . date("Y-m-d", $end_time - 3600 * 24));
			}
			
			$this->smarty->assign("active_list", json_encode($active_list));
			$this->smarty->assign("action", '用户活跃度统计');
			$this->smarty->assign("type", $type);
			$this->smarty->assign("limit", $limit);
			$this->smarty->display("user/active.html");
		}

		function active_list($start_time = 0, $end_time = 0, $limit = 0){
			if($_POST){
				$post_data = $this->input->post();
				$start_time = !empty($post_data['start_time']) ? $post_data['start_time'] : 0;
				$end_time = !empty($post_data['end_time']) ? $post_data['end_time'] : 0;
			}
			$conditions['start_time'] = $start_time;
			$conditions['end_time'] = $end_time;

			$this->load->model("User_visit_statistics");
			$result = $this->User_visit_statistics->get_visit_info(0, $conditions, $limit, $this->offset);
			$visit_statistics_info = $result['result'];
			$today_time = strtotime(date("Y-m-d", time()));
			foreach ($visit_statistics_info as $k => $v) {
				if($v['user_reg_time'] > $today_time){
					$visit_statistics_info[$k]['is_new_user'] = 1;
				}else{
					$visit_statistics_info[$k]['is_new_user'] = 0;
				}
				$visit_statistics_info[$k]['visit_url'] = explode("&#&", $v['visit_url']);
				if(count($visit_statistics_info[$k]['visit_url']) > 0){
					foreach ($visit_statistics_info[$k]['visit_url'] as $key => $value) {
						$visit_statistics_info[$k]['visit_url'][$key] = explode("|", $value);
						$sortdistance[$k][$key] = strtotime($visit_statistics_info[$k]['visit_url'][$key][1]);
					}
					array_multisort($sortdistance[$k], SORT_DESC, $visit_statistics_info[$k]['visit_url']);
				}
			}
			$this->load->library('pagination');
			$page_config['base_url'] = '/user/active_list/'.$start_time.'/'.$end_time;
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $this->offset; 
			$page_config['uri_segment'] = 5;
			$this->pagination->initialize($page_config); 
			$pagination = $this->pagination->create_links();

			// print_r($visit_statistics_info);
			$this->smarty->assign("action", '活跃用户');
			$this->smarty->assign("conditions", $conditions);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->assign("visit_statistics_info", $visit_statistics_info);
			$this->smarty->display("user/active_list.html");
		}
	}